<?php

namespace App\Repositories\HomeTemplates;

use App\Criteria\HomeTemplateCriteria;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Models\HomeTemplate\HomeTemplate;

/**
 * Class HomeTemplateRepositoryEloquent.
 *
 * @package namespace App\Repositories;
 */
class HomeTemplateRepositoryEloquent extends BaseRepository implements HomeTemplateRepository
{

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return HomeTemplate::class;
    }

    

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * 获取当前可用首页
     * @author Jerry
     * @param int $id
     * @return array
     */
    public function findHomeParentById(int $id)
    {
        $time = time();

        $where = [
            'parent_id' =>  $id,
            'status'    =>  1,
            ['start_time', '<=', $time],
            ['end_time', '>', $time]
        ];

        $template = $this->findWhere($where);

        if ($template->isEmpty()) {
            return [];
        }

        return $template->first();
    }
    
}
